#include "api_weather.h"

namespace Api
{

    void Weather::call()
    {
        HTTPClient http;

        http.begin(apiUrl);
        int httpCode = http.GET();
        if (httpCode != HTTP_CODE_OK)
        {
            Serial2.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
            http.end();
            throw Error::HttpException();
        }
        String payload = http.getString();
        DeserializationError error = deserializeJson(doc, payload, DeserializationOption::NestingLimit(20));
        if (error)
        {
            Serial2.println("JSON parsing failed.");
            http.end();
            throw Error::HttpException();
        }
        http.end();
    }
    Api::Data Weather::getData()
    {
        Api::Data _data;
        _data.loadWeather(doc);
        return _data;
    }

}
